package net.zjitc.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.minio.MinioClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.zjitc.common.ResponseResult;
import net.zjitc.constant.RedisConstant;
import net.zjitc.entity.User;
import net.zjitc.entity.Yonghu;
import net.zjitc.service.ICarouseService;
import net.zjitc.service.IUserService;
import net.zjitc.service.IYonghuService;
import net.zjitc.until.MinioUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @auther CodeGenerator-gmt
 * @create 2022-04-25 11:00:47
 * @describe 前端控制器
 */
@CrossOrigin
@RestController
@Api(tags = "用户管理列表")
public class YonghuController {
    @Reference
    private IYonghuService yonghuService;

    @Reference
    private IUserService userService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    ResponseResult result=new ResponseResult();

    @Value("${minio.bucketName}")
    private String bucketName;
    @Autowired
    private MinioClient minioClient;
    @Autowired
    private MinioUtils minioutils;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    @ApiOperation("上传头像")
    @PostMapping("yonghu/upload")
    public ResponseResult upload(@RequestParam("imgFile") MultipartFile imgFile){

        System.out.println(imgFile);
        try {
            List<String> upload = minioutils.upload(new MultipartFile[]{imgFile});
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_RESOURCES,upload.get(0));
            result.setSuccess("图片上传成功",upload.get(0));
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setError("图片上传失败");
            return result;
        }
    }

    @ApiOperation("获取所有用户信息")
    @GetMapping("yonghu")
    public ResponseResult getYonghus(@RequestParam(required = false,value ="querystring" ) String querystring,
                                     @RequestParam(required = true ,value = "pagenum") Integer pagenum,
                                     @RequestParam(required = true,value = "pagesize") Integer pagesize){
        Page<Yonghu> yonghu=yonghuService.getYonghus(querystring,pagenum,pagesize);
        if (yonghu.getTotal()>0){
            result.setSuccess("获取用户成功",yonghu);
        }else {
            result.setError("获取用户失败");
        }
        return result;
    }

    @ApiOperation("根据id查找用户")
    @GetMapping("yonghu/{id}")
    public ResponseResult getYonghuById(@PathVariable("id") Integer id){
        Yonghu yonghu = yonghuService.getById(id);
        if (yonghu!=null){
            result.setSuccess("获取用户成功",yonghu);
        }else {
            result.setError("获取用户失败");
        }
        return result;
    }

    @ApiOperation("注册用户")
    @PostMapping("regyonghu")
    public ResponseResult Addyonghu(@RequestBody Yonghu yonghu){
        User user = userService.findByUsername(yonghu.getUsername());
        if (user==null) {
            User user1=new User();
            String pwd = passwordEncoder.encode(yonghu.getPassword());
            Date time = new Date();
            yonghu.setPassword(pwd);
            yonghu.setAddtime(time);
            boolean save = yonghuService.save(yonghu);
            Yonghu yonghu1= yonghuService.selectByUsername(yonghu.getUsername());

            user1.setUsername(yonghu1.getUsername());
            user1.setPassword(pwd);
            user1.setActorid(yonghu1.getId());
            user1.setRoleid(2);
            user1.setCreateTime(time);
            boolean save1 = userService.save(user1);

            if (save&&save1){
                result.setCreateSuccess("添加用户成功");
            }
        }else {
            result.setError("账号重复");

        }

        return result;

    }

    @ApiOperation("修改用户")
    @PutMapping("yonghu/{id}")
    public ResponseResult Updateyonghu(@RequestBody Yonghu yonghu,@PathVariable("id") int id){
        yonghu.setId(id);
        boolean update = yonghuService.updateById(yonghu);
        if (update){
            result.setSuccess("修改用户成功");
        }else {
            result.setError("修改用户失败");
        }
        return result;
    }

    @ApiOperation("删除用户")
    @DeleteMapping("yonghu/{id}")
    public ResponseResult DeleteYonghu(@PathVariable("id") int id){

        int delete= yonghuService.deleteById(id);
        if (delete!=0){
            result.setSuccess("删除用户成功");
        }else {
            result.setError("删除用户失败");
        }
        return result;
    }

    @ApiOperation("修改用户密码")
    @GetMapping("/updateYonghuPwd")
    public ResponseResult updatePassword(@RequestParam(required = false) String username,
                                         @RequestParam(required = false) String pwd1,
                                         @RequestParam(required = false) String pwd2,
                                         @RequestParam(required = false) String pwd3){
        //加解密方法
        BCryptPasswordEncoder bc = new BCryptPasswordEncoder();
        Yonghu yonghu = yonghuService.selectByUsername(username);
        User user = userService.findByUsername(username);
        //输入密码与数据库密码比较
        boolean matches = bc.matches(pwd1,user.getPassword());
        if (!matches){
            result.setError("旧密码不匹配");
        }else {
            String pwd = passwordEncoder.encode(pwd2);
            user.setPassword(pwd);
            yonghu.setPassword(pwd);
            boolean updateuser = userService.updateById(user);
            boolean updateyonghu = yonghuService.updateById(yonghu);
            if (updateuser&&updateyonghu){
                result.setSuccess("修改密码成功");
            }else {
                result.setError("修改密码失败");
            }
        }
        return result;
    }
}

